
varying vec4  viewCoords;
varying vec3  mixture;
varying vec3  diffuseColor;
varying float fog;
varying float fade;

const vec3 SkyColor = vec3(0.9882 , 0.9609 , 0.8164);
const vec3 HazeColor = vec3(0.49 , 0.495 , 0.49);

const float C1 = 0.429043;
const float C2 = 0.511664;
const float C3 = 0.743125;
const float C4 = 0.886227;
const float C5 = 0.247708;

const vec3 L00  = vec3( 1.0351604,  0.7603549,  0.7074635);
const vec3 L1m1 = vec3( 0.4442150,  0.3430402,  0.3403777);
const vec3 L10  = vec3(-0.2247797, -0.1828517, -0.1705181);
const vec3 L11  = vec3( 0.7110400,  0.5423169,  0.5587956);
const vec3 L2m2 = vec3( 0.6430452,  0.4971454,  0.5156357);
const vec3 L2m1 = vec3(-0.1150112, -0.0936603, -0.0839287);
const vec3 L20  = vec3(-0.3742487, -0.2755962, -0.2875017);
const vec3 L21  = vec3(-0.1694954, -0.1343096, -0.1335315);
const vec3 L22  = vec3( 0.5515260,  0.4222179,  0.4162488);

void main( void )
{
	gl_Vertex.y    += gl_Color.a;
    gl_Position     = ftransform();
    viewCoords      = gl_ModelViewProjectionMatrix * gl_Vertex;
    vec3 tnorm      = normalize(gl_Normal);
    vec3 ecPosition = vec3 (gl_ModelViewMatrix * gl_Vertex);
    vec3 lightVec   = normalize(vec3(2000,2000,1000) - ecPosition);
    float cosTheta  = dot(tnorm, lightVec);
    float a         = 0.5 + 0.5 * cosTheta;


    diffuseColor = vec3(mix(HazeColor , SkyColor , a));
    diffuseColor   *= C1 * L22 * (tnorm.x * tnorm.x - tnorm.y * tnorm.y) +
                      C3 * L20 * tnorm.z * tnorm.z + C4 * L00 - C5 * L20 +
                      2.0 * C1 * L2m2 * tnorm.x * tnorm.y +
                      2.0 * C1 * L21  * tnorm.x * tnorm.z +
                      2.0 * C1 * L2m1 * tnorm.y * tnorm.z +
                      2.0 * C2 * L11  * tnorm.x +
                      2.0 * C2 * L1m1 * tnorm.y +
                      2.0 * C2 * L10  * tnorm.z;
	mixture = gl_Color.rgb;
    fog = 1.0-clamp(pow((viewCoords.z-gl_Fog.start)*gl_Fog.scale, 
    		1.0),0.0,1.0)*0.75;
    fade = 1.0-clamp(pow((viewCoords.z-gl_Fog.start)*gl_Fog.scale, 
    		20.0),0.0,1.0);
    		
	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}